package cn.wolfcode.web.controller;

import cn.wolfcode.web.domain.Permission;
import cn.wolfcode.web.domain.Role;
import cn.wolfcode.web.query.QueryObject;
import cn.wolfcode.web.service.IPermissionService;
import cn.wolfcode.web.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo) {
        List<Role> roles = roleService.listAll();
        PageInfo pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("roleList", roles);
        return "role/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            roleService.delete(id);
        }
        return "redirect:/role/list.do";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model) {
        System.out.println("11");
//        查询所有权限对象放在角色保存页面显示
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions",permissions);
        if (id!=null) {
            Role role = roleService.get(id);
            model.addAttribute("role",role);
        }
        return "role/input";
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Model model) {
        System.out.println("11");
        if(role.getId()!=null){
            roleService.update(role);
        }else{
            roleService.save(role);
        }

        return "redirect:/role/list.do";//"/role/saveOrUpdate.do"
    }
}
